<template>
  <el-dialog
    title="切换组织"
    ref="selectOrg"
    :visible.sync="$store.state.user.openSelectOrg"
    width="850px"
    append-to-body
    @open="handleOpen"
    @close="handleClose"
    :close-on-press-escape="false"
    :show-close="false"
    class="branchDialog2"
  >
    <el-row class="org-list">
      <el-col :span="8" v-for="(item,index) in $store.getters.deptList" :key="index">
        <el-card
          shadow="hover"
          :style="(item.ctBranchCategory=='ZZLB631'||item.ctBranchCategory=='ZZLB632')?bgcolor1:bgcolor2"
          @click.native="SelectOrg(item.deptId)"
        >
          <el-col :span="24">{{item.deptName}}</el-col>
        </el-card>
      </el-col>
    </el-row>
    <!--       
          :style="item.ctBranchCategory=='ZZLB611'?bgcolor1:item.ctBranchCategory=='ZZLB621'?bgcolor2:bgcolor3"
      <el-row class="org-list">
      <el-col :span="24" v-for="(item,index) in $store.getters.deptList" :key="item.deptId">
        <el-card shadow="hover">
          <el-row :gutter="12">
            <el-col :span="20">{{item.deptName}}{{item.deptId==defaultDeptId?"--(默认选中组织)":''}}</el-col>

            <el-col :span="4">
              <el-radio v-model="radio" :label="item.deptId" border>点击选择</el-radio>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
       <el-col :span="24">
        <el-card shadow="hover">
          <el-row :gutter="12">
            <el-col :span="20">12</el-col>
            <el-col :span="4">
              <el-radio v-model="radio" label="2" border>点击选择</el-radio>
            </el-col>
          </el-row>
        </el-card>
      </el-col>
    </el-row>-->
    <div slot="footer">
      <el-button type="default" @click.native.prevent="handleClose">取消</el-button>
      <!-- <el-button type="primary" @click.native.prevent="SelectOrg">提交选中组织</el-button> -->
    </div>
  </el-dialog>
</template>

<script>
import { switchDept } from '@/api/system/user'
import { resetRouter } from '@/router'
import { setToken } from '@/utils/auth'
export default {
  name: 'selectOrg',
  // props: {
  //   openSelectOrg: {
  //     type: Boolean,
  //     default: false
  //   }
  // },
  inject: ['reload'],
  data() {
    return {
      defaultDeptId: this.$store.getters.deptId,
      radio: this.$store.getters.deptId,
      bgcolor1: 'background:#F6FDFF',
      bgcolor2: 'background:#FFF9F8',
      bgcolor3: 'background:#FDFFF8',
    }
  },
  computed: {
    // radio() {
    //   return this.$store.getters.dept.deptId
    // },
    // defaultDeptId() {
    //   return this.$store.getters.dept.deptId
    // },
  },
  //   watch:{
  //   defaultDeptId(val, oldVal){
  //    },
  // },
  //   watch: {
  //   defaultDeptId: {
  //     handler(newValue, oldValue) {},
  //     deep: true,
  //   }
  // },
  mounted() {},

  methods: {
    handleOpen() {
      this.defaultDeptId = this.$store.getters.deptId
    },
    SelectOrg(deptId) {
      //if (this.defaultDeptId == this.radio) {
      if (this.defaultDeptId == deptId) {
        this.$store.dispatch('setOpenSelectOrg', false)
      } else {
        // switchDept({ deptId: this.radio, deptName: '' }).then((response) => {
        switchDept({ deptId: deptId, deptName: '' }).then((response) => {
          this.msgSuccess('修改成功')
          setToken(response.token)
          this.$store.dispatch('setToken', response.token)
          this.$store.dispatch('setOpenSelectOrg', false)
          this.$store
            .dispatch('GetInfo')
            .then(() => {
              this.$store.dispatch('GenerateRoutes').then((accessRoutes) => {
                // 根据roles权限生成可访问的路由表
                resetRouter(accessRoutes)
                this.$router.addRoutes(accessRoutes) // 动态添加可访问路由表
                // next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
                this.$store.dispatch('tagsView/delAllViews')
                this.$router.push('/')
                this.reload()
              })
            })
            .catch((err) => {
              // debugger
              this.$store.dispatch('LogOut').then(() => {
                this.$message.error(err)
                if (this.$store.state.spsFirst.casLoginFlag) {
                  location.href = `${defaultSettings.casPath}/cas/logout`
                } else {
                  location.href = `${defaultSettings.index}` + '#/index'
                }
                // this.$router.push('/')
              })
            })
        })
      }
      // this.$store.dispatch("setOpenSelectOrg", false);
    },
    handleClose() {
      this.$store.dispatch('setOpenSelectOrg', false)
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.org-list > .el-col {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.branchDialog2 {
  .el-card__body {
    display: table-cell;
    vertical-align: middle;
  }
  .el-card {
    cursor: pointer;
    display: table;
    width: 95%;
    height: 68px;
    text-align: center;
    margin: 10px;
    padding: 10px;
  }
  .el-card:hover {
    color: #d65c5c;
    border-color: #d65c5c;
    background-color: #fdf0f0 !important;
  }
}
</style>
